package com.huawei.esdk.tp.authorize;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.cxf.common.util.StringUtils;

import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.session.itf.ISessionMgr;

public final class SessionManager
{
    private static Map<String, SessionInfo> sessionMap = new ConcurrentHashMap<String, SessionInfo>();
    
    private ISessionMgr sessionMgrPlatform = ApplicationContextUtil.getBean("sessionMgrC50");
    
    private static SessionManager instance = new SessionManager();
    
    private SessionManager()
    {
    }
    
    public static synchronized SessionManager getInstance()
    {
        return instance;
    }
    
    public synchronized boolean saveSDKSession(String sdkSession)
    {
        return saveSDKSession(sdkSession, null);
    }
    
    public synchronized boolean saveSDKSession(String sdkSession, SessionInfo info)
    {
        if (null == sdkSession)
        {
            return false;
        }
        
        if (null == info)
        {
            info = sessionMap.get(sdkSession);
            if (null == info)
            {
                info = new SessionInfo();
            }
        }
        
        sessionMap.put(sdkSession, info);
        
        return true;
    }
    
    public synchronized SessionInfo getSDKSession(String sdkSession)
    {
        return sessionMap.get(sdkSession);
    }
    
    public synchronized SessionInfo getSDKSessionAvoidNull(String sdkSession)
    {
        SessionInfo ds = sessionMap.get(sdkSession);
        if (null == ds)
        {
            ds = new SessionInfo();
            sessionMap.put(sdkSession, ds);
        }
        
        return ds;
    }
    
    public synchronized boolean isSDKSessionExists(String sdkSession)
    {
        if (StringUtils.isEmpty(sdkSession))
        {
            return false;
        }
        return sessionMap.containsKey(sdkSession);
    }
    
    public synchronized void removeSDKSession(String sdkSession)
    {
        if (null == sdkSession)
        {
            return;
        }
        sessionMgrPlatform.removeSDKSession(sdkSession);
        try
        {
            if (!sessionMap.containsKey(sdkSession))
                return;
            sessionMap.remove(sdkSession);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public synchronized boolean saveSecretKey(String sdkSession, byte[] secretKey)
    {
        return saveSecretKey(sdkSession, secretKey, null);
    }
    
    public synchronized boolean saveSecretKey(String sdkSession, byte[] secretKey, byte[] iv)
    {
        if (null == sdkSession)
        {
            return false;
        }
        
        SessionInfo info = sessionMap.get(sdkSession);
        if (null == info)
        {
            info = new SessionInfo();
        }
        info.setSecretKey(secretKey);
        info.setIv(iv);
        
        sessionMap.put(sdkSession, info);
        return true;
    }
}
